import 'dart:math';

import 'package:aero_frisbee/app/model/af_block_list_model/af_block_list_model.dart';
import 'package:aero_frisbee/app/model/af_short_link_model/af_short_link_model.dart';
import 'package:aero_frisbee/gen/assets.gen.dart';
import 'package:aero_frisbee/pages/modues/message/pages/followed/view_model/af_followed_list_view_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'view_model/af_video_view_model.dart';
import 'views/af_video_cell.dart';

class AfVideoPage extends ConsumerStatefulWidget {
  const AfVideoPage({super.key});
  @override
  ConsumerState<ConsumerStatefulWidget> createState() => _AfVideoPageState();
}

class _AfVideoPageState extends ConsumerState<AfVideoPage>
    with AutomaticKeepAliveClientMixin {
  final PageController _pageController = PageController();

  @override
  bool get wantKeepAlive => true;
  @override
  Widget build(BuildContext context) {
    super.build(context);
    final blockList = ref
        .watch(getBlockListFutureProvider)
        .maybeWhen(
          orElse: () => List<AfBlockListModel>.from([]),
          data: (data) => data,
        )
        .map((e) => e.broadcasterId ?? '')
        .toList();
    return Scaffold(
      backgroundColor: const Color(0xff333333),
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        systemOverlayStyle: SystemUiOverlayStyle.light,
        leadingWidth: 220,
        leading: Align(
          alignment: Alignment.bottomLeft,
          child: Padding(
            padding: const EdgeInsets.only(left: 16),
            child: Assets.icons.iconVideoTitle.image(width: 70),
          ),
        ),
      ),
      body: ref.watch(afVideoListServiceProvider).maybeWhen(
            orElse: () => Container(),
            loading: () => const Center(child: CircularProgressIndicator()),
            data: (data) => _buildBody(data
                .where((e) =>
                    !blockList.contains(e.cacheBind?.homeUser?.userId ?? ''))
                .toList()),
          ),
    );
  }

  List<AfShortLinkModel> shuffleList(List<AfShortLinkModel> list) {
    final List<AfShortLinkModel> shuffledList =
        List<AfShortLinkModel>.from(list);
    final Random random = Random();
    for (int i = shuffledList.length - 1; i > 0; i--) {
      final int n = random.nextInt(i + 1);
      final AfShortLinkModel temp = shuffledList[i];
      shuffledList[i] = shuffledList[n];
      shuffledList[n] = temp;
    }
    return shuffledList;
  }

  _buildBody(List<AfShortLinkModel> data) {
    return Container(
      margin: const EdgeInsets.only(bottom: 30),
      child: PageView.builder(
        key: const Key('home'),
        physics: const QuickerScrollPhysics(),
        controller: _pageController,
        scrollDirection: Axis.vertical,
        itemCount: data.length,
        itemBuilder: (context, i) {
          return AfVideoCell(
            item: data[i],
          );
        },
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didUpdateWidget(AfVideoPage oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
}

class QuickerScrollPhysics extends BouncingScrollPhysics {
  const QuickerScrollPhysics({ScrollPhysics? parent}) : super(parent: parent);

  @override
  QuickerScrollPhysics applyTo(ScrollPhysics? ancestor) {
    return QuickerScrollPhysics(parent: buildParent(ancestor));
  }

  @override
  SpringDescription get spring => SpringDescription.withDampingRatio(
        mass: 0.2,
        stiffness: 300.0,
        ratio: 1.1,
      );
}
